<template>
  <!-- 白名单规则管理 -->
  <div class="container">
    <el-row v-if="isCanBack" class="go-back">
      <el-col>
        <el-button class="fr" size="mini" type="success" @click="$router.go(-1)"
          >返回上一页</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="20">
        <el-input
          v-model="query.memberId"
          placeholder="请输入会员ID"
          size="small"
          class="width-180"
          clearable
        />
        <el-input
          v-model="query.nickName"
          placeholder="请输入会员昵称"
          size="small"
          class="width-180"
          clearable
        />
        <el-input
          v-model="query.tel"
          placeholder="请输入手机号码"
          size="small"
          class="width-180"
          clearable
        />
        <el-input
          v-model="query.ruleNameFragment"
          placeholder="请输入触犯规则关键字"
          size="small"
          class="width-180"
          clearable
        />
        <el-date-picker
          v-model="searchDate"
          :picker-options="pickerOptions"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
          @change="dateChange"
        />
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="search('search')"
          >搜索</el-button
        >
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      ref="defaultRulesTable"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width: 100%;margin-top:15px;"
    >
      <el-table-column label="编号" width="60" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column
        label="会员ID"
        align="center"
        width="120"
        prop="memberId"
      />
      <el-table-column label="会员昵称" align="center" prop="nickName" />
      <el-table-column label="错判规则">
        <template slot-scope="scope">
          <span :title="scope.row.allRules">
            {{ scope.row.firstRuleGroupName }}
            {{ scope.row.combineRuleEntityList.length > 1 ? '...' : '' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作员"
        align="center"
        width="120"
        prop="operator"
      />
      <el-table-column label="开始保护时间" align="center" prop="updateTime" />
      <el-table-column
        label="保护时长"
        align="center"
        width="100"
        prop="updateTime"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.protectDays <= 30">
            {{ scope.row.protectDays }} 天</span
          >
          <span v-else>永久</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证" align="center" prop="id_num" />
      <el-table-column label="手机号码" align="center" prop="tel" />
      <el-table-column label="零钱余额($)" align="center" prop="smallMoney" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="goToDetail(scope.row.memberId)"
            >会员详情</el-button
          >
          <el-button
            type="danger"
            plain
            size="mini"
            @click="beforeDeleteRule(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row style="text-align:right;">
      <Pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageIndex"
        :limit.sync="query.pageSize"
        @pagination="search"
      />
    </el-row>
  </div>
</template>
<script>
import {
  getWhiteList,
  delWhite
} from '@/api/user-exception-filtering/abnormal-behavior';
import Pagination from '@/components/Pagination/index.vue';
import { changeStatusJS, watchRouterJs } from '../mixins/common.js';

export default {
  name: 'DefaultRules',

  components: {
    Pagination
  },

  mixins: [changeStatusJS, watchRouterJs],

  data() {
    return {
      pageName: '会员异常筛选 / 白名单规则管理',
      tableData: [],
      listLoading: false,
      total: 0,
      searchDate: [],
      query: {
        memberId: '',
        nickName: '',
        tel: '',
        ruleNameFragment: '',
        beginTime: '',
        endTime: '',
        pageIndex: 1,
        pageSize: 50
      },
      // 时间选择限制
      pickerMinDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.pickerMinDate = minDate.getTime();
          if (maxDate) {
            this.pickerMinDate = '';
          }
        },
        disabledDate: time => time.getTime() > Date.now()
      },
      isCanBack: false
    };
  },

  activated() {
    if (!this.isCache) {
      this.query = {
        memberId: '',
        nickName: '',
        tel: '',
        ruleNameFragment: '',
        beginTime: '',
        endTime: '',
        pageIndex: 1,
        pageSize: 50
      };
      this.query.memberId =
        this.$route.query.memberId == undefined
          ? ''
          : this.$route.query.memberId;
      this.isCanBack = !!this.$route.query.memberId;

      // 分页和搜索
      this.getData(this.query);
    }
  },

  methods: {
    getData() {
      this.listLoading = true;
      getWhiteList(this.query)
        .then(result => {
          if (result.code === 200) {
            this.listLoading = false;
            this.tableData = result.data.map(item => {
              item.ruleGroupNameList = item.combineRuleEntityList;
              item.updateTime =
                item.updateTime === undefined
                  ? ''
                  : item.updateTime.split('T').join(' ');
              return item;
            });
            this.tableData = this.formatRule(
              JSON.parse(JSON.stringify(this.tableData))
            );
            this.total = result.count;
          } else {
            this.listLoading = false;
            this.$message.error(result.msg);
          }
        })
        .catch(() => {
          this.listLoading = false;
        });
    },

    async beforeDeleteRule(data) {
      const action = await this.$confirm('确定删除该规则吗？', '提示', {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      });

      if (action == 'confirm') {
        this.delWhite(data);
      }
    },

    search(type) {
      this.query.pageIndex = type == 'search' ? 1 : this.query.pageIndex;
      this.getData(this.query);
    },

    delWhite(data) {
      const query = {
        whiteList: [
          {
            memberId: data.memberId,
            ruleId: data.id,
            ruleName: data.combineRuleEntityList[0].ruleName
          }
        ],
        page: this.pageName
      };
      delWhite(query)
        .then(result => {
          if (result.code == 200) {
            this.$message.success('删除成功');
            this.getData(this.query);
          } else {
            this.$message.error(result.msg);
          }
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>
.container {
  padding: 20px;
  height: calc(100vh-50px);
  overflow-y: auto;
}
.text-align--right {
  text-align: right;
}
.width {
  &-180 {
    width: 180px;
  }
}
</style>
